/**
 *
 */
package com.angel.common.providers.managers;

import java.lang.reflect.Method;

import com.angel.annotation.exceptions.TooManyAnnotationException;
import com.angel.common.helpers.ReflectionHelper;
import com.angel.common.providers.annotations.Identificator;


/**
 * @author William
 *
 */
public class ObjectProviderNameAnnotationManager {

	private static final Class<Identificator> OBJECT_PROVIDER_NAME_ANNOTATION = Identificator.class;

	public ObjectProviderNameAnnotationManager(){
		super();
	}

	public boolean canIdentifyObjectByName(Class<?> objectProviderClass) {
		Method[] methods = ReflectionHelper.getAllMethodWith(OBJECT_PROVIDER_NAME_ANNOTATION, objectProviderClass);
		boolean canIdentifyObjectByName = false;
		if(methods != null && methods.length == 1){
			canIdentifyObjectByName = true;
		}

		if(!canIdentifyObjectByName && methods != null && methods.length > 1){
			throw new TooManyAnnotationException("Annotation [" + OBJECT_PROVIDER_NAME_ANNOTATION.getSimpleName() + "] was used in too many methods [" + methods.length + "] and it must be used at only one.");
		}
		return canIdentifyObjectByName;
	}

	public String getIdentifierNameFor(Object object) {
		Method[] methods = ReflectionHelper.getAllMethodWith(OBJECT_PROVIDER_NAME_ANNOTATION, object.getClass());
		String value = null;
		if(methods!= null && methods.length == 1 && methods[0].getParameterTypes().length == 0){
			value = ReflectionHelper.invokeMethodWithoutParameters(object, methods[0]).toString();
		}
		return value;
	}

}
